#include "FolderParse.h"

	void FileGetter::ToLower(char* a_in, char* a_out)
	{
		char temp;
		int count = 0;
		temp = a_in[count];
		while(a_in[count] != '\0')
		{
			if(temp>= 'A' && temp<='Z')
				a_out[count] = temp+('a'-'A');
			else
				a_out[count] = temp;
			temp = a_in[++count];
		}
	}

	FileGetter::~FileGetter()
	{
		release();
	}

	void FileGetter::parseFolder(TemplateVector<char *> & a_folder, char* folder,char* a_type, bool addFolderPath)
	{
		WIN32_FIND_DATAA found; 
		HANDLE hfind;
		char folderstar[256];

		ZeroMemory(&folderstar,256);
		sprintf_s(folderstar,"%s*%s",folder,a_type);

		hfind = FindFirstFileA(folderstar,&found);
		if(hfind == (HANDLE)0xffffffff)
			return;
		a_folder.clear();
		char* first = new char[256];
		if(addFolderPath)
		{
			char temp[256]; ZeroMemory(&temp,256);
			sprintf_s(temp, "%s%s",folder, found.cFileName);
			strcpy_s(first,256,temp);
		}
		else
		{
			char temp[256]; ZeroMemory(&temp,256);
			sprintf_s(temp, "%s", found.cFileName);
			strcpy_s(first,256,temp);
		}
		a_folder.add(first);
		int check = 1;
		while(check){

			char* temp = new char[260];
			check = FindNextFileA(hfind,&found);
			DWORD error;
			error = GetLastError();
			if (check)
			{
				if(addFolderPath)
				{
					char tempLine[256]; ZeroMemory(&tempLine,256);
					sprintf_s(tempLine, "%s%s",folder, found.cFileName);
					strcpy_s(temp,256,tempLine);
				}
				else
				{
					char tempLine[256]; ZeroMemory(&tempLine,256);
					sprintf_s(tempLine, "%s", found.cFileName);
					strcpy_s(temp,256,tempLine);
				}
				a_folder.add(temp);
			}
			if(!check)
				delete []temp;
		}
	}

	//Returns the parsed folder sorted
	TemplateVector<char *> FileGetter::sortFolder(TemplateVector<char *> & a_folder, char* folder,char* a_type, bool addFolderPath)
	{
		parseFolder(a_folder, folder, a_type, addFolderPath);
		TemplateVector<char*> temp;
		bool running = true;
		char * Atemp;
		char * Btemp;
		int cap;
		int floor;
		while(a_folder.size()!=temp.size())
		{
			for (int i = 0; i < a_folder.size(); i++)
			{
				Atemp = a_folder[i];
				cap = temp.size()-1;
				floor = 0;
				while(1)
				{
					if(temp.size())
					{
						Btemp = temp[((cap-floor)/2)+floor];

						int cmp;
						char tempA[260];
						char tempB[260];
						ZeroMemory(tempA,260);
						ZeroMemory(tempB,260);
						ToLower(Atemp,tempA);
						ToLower(Btemp,tempB);
						cmp = strcmp(tempA,tempB);
						if(cmp <0)
						{
							if(floor == cap)
							{
								temp.insert(cap,Atemp);
								break;
							}
							if(cap - floor == 1)
							{
								cap = floor;
							}	
							else
								cap = ((cap-floor)/2)+floor;
						}
						else
						{
							if(floor == cap)
							{
								temp.insert(cap+1,Atemp);
								break;
							}
							if(cap - floor == 1)
							{
								floor = cap;
							}
							else
								floor = ((cap-floor)/2)+floor;
						}
					}
					else
					{
						temp.add(a_folder[i]);
						break;
					}
				}
			}
		}
		return temp;
	}